<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta name="google-site-verification" content="-YPbgjltFVFljoCm04Ktk0m38A8CkQxpifuKVZXtawA">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">






















<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/avatar.gif?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/avatar.gif?v=5.1.4" color="#222">





  <meta name="keywords" content="java,algorithm,">





  <link rel="alternate" href="/blog/atom.xml" title="lfagon" type="application/atom+xml">






<meta name="description" content="二叉树可以使用两种存储结构： 顺序存储和二叉链表。在使用二叉链表的存储结构的过程中，会存在大量的空指针域，为了充分利用这些空指针域，引申出了“线索二叉树”。  对于一个有 n 个节点的二叉链表，每个节点有指向左，点的 2 个指针域，整个二叉链表存在 2n 个指针域。n 个节点的二叉链表，n-1 条分支线，那么空指针域的个数 =2n-(，1) = n+1 个空指针域，从存储空间的角度来看，这 n+1">
<meta name="keywords" content="java,algorithm">
<meta property="og:type" content="article">
<meta property="og:title" content="线索二叉树">
<meta property="og:url" content="http://lfagon.gitee.io/blog/2018/05/16/线索二叉树/index.html">
<meta property="og:site_name" content="lfagon">
<meta property="og:description" content="二叉树可以使用两种存储结构： 顺序存储和二叉链表。在使用二叉链表的存储结构的过程中，会存在大量的空指针域，为了充分利用这些空指针域，引申出了“线索二叉树”。  对于一个有 n 个节点的二叉链表，每个节点有指向左，点的 2 个指针域，整个二叉链表存在 2n 个指针域。n 个节点的二叉链表，n-1 条分支线，那么空指针域的个数 =2n-(，1) = n+1 个空指针域，从存储空间的角度来看，这 n+1">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-21T07:16:26.324Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="线索二叉树">
<meta name="twitter:description" content="二叉树可以使用两种存储结构： 顺序存储和二叉链表。在使用二叉链表的存储结构的过程中，会存在大量的空指针域，为了充分利用这些空指针域，引申出了“线索二叉树”。  对于一个有 n 个节点的二叉链表，每个节点有指向左，点的 2 个指针域，整个二叉链表存在 2n 个指针域。n 个节点的二叉链表，n-1 条分支线，那么空指针域的个数 =2n-(，1) = n+1 个空指针域，从存储空间的角度来看，这 n+1">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://lfagon.gitee.io/blog/2018/05/16/线索二叉树/">





  <title>线索二叉树 | lfagon</title>
  








  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-139884627-1"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-139884627-1');
  </script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">

    <div class="headband"></div>
    

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <!-- <span class="logo-line-before"><i></i></span> -->
        <!-- <span class="site-title">lfagon</span> -->
	     <img src="/blog/images/logo.png">
        <!-- <span class="logo-line-after"><i></i></span> -->
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">个人博客</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-top">
          <a href="/blog/top/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-signal"></i> <br>
            
            热门
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/blog/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tracks">
          <a href="/blog/tracks" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-comment"></i> <br>
            
            留言
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://lfagon.gitee.io/blog/blog/2018/05/16/线索二叉树/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="付祥/lfagon">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lfagon">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">线索二叉树</h2>
        

        <div class="post-meta">
	    
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <font color="blue"><i class="fa fa-calendar-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-16T11:02:03+08:00">
                2018-05-16
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                 <font color="#40E0D0"><i class="fa fa-calendar-check-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-06-21T15:16:26+08:00">
                2019-06-21
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <font color="red"><i class="fa fa-folder-o"></i></font>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <font color="black"><span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/介绍/" itemprop="url" rel="index">
                    <span itemprop="name">介绍</span>
                  </a>
                </span></font>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2018/05/16/线索二叉树/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/blog/2018/05/16/线索二叉树/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.7k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>二叉树可以使用两种存储结构： 顺序存储和二叉链表。在使用二叉链表的存储结构的过程中，会存在大量的空指针域，为了充分利用这些空指针域，引申出了“线索二叉树”。</p>
<ul>
<li>对于一个有 n 个节点的二叉链表，每个节点有指向左，点的 2 个指针域，整个二叉链表存在 2n 个指针域。n 个节点的二叉链表，n-1 条分支线，那么空指针域的个数 =2n-(，1) = n+1 个空指针域，从存储空间的角度来看，这 n+1 个，针域浪费了内存，</li>
<li>在原来二叉树中每次遍历都需要进行递归（，树非常大，是非常耗内存的）或者，递归方式（虽然这个没有递归耗内存，但是你不觉的麻烦吗），有了线索二叉树只需第一次遍历就能构建出线索二叉树，其余时候遍历二叉树就跟遍历链表一样，我们用二个标记来标记左右指针，左指针指向前驱，右指针指向后驱</li>
<li>我们把这种指向前驱和后继的指针成为线索，加上线索的二叉链表成为线索链表，对应的二叉树就成为“线索二叉树（Threaded Binary Tree)”</li>
</ul>
<hr>
<h2>代码</h2>
<p>我会在中加上注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.concurrent. LinkedTransferQueue;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BinaryTreeThread</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		String []a=&#123;<span class="string">"A"</span>, <span class="string">"B"</span>, <span class="string">"D"</span>, <span class="string">"C"</span>, <span class="string">"E"</span>&#125;;</span><br><span class="line">		BinaryTreeThread&lt;String&gt; tree=<span class="keyword">new</span> BinaryTreeThread&lt;String&gt;();</span><br><span class="line">		System.out.println(<span class="string">"?"</span>);</span><br><span class="line">		tree.createtBinaryTree(a, BinaryTreeThread. CREATE_FOOR);</span><br><span class="line"></span><br><span class="line">		tree.preThread();</span><br><span class="line">		tree.preTraversal();</span><br><span class="line">		<span class="comment">//System.out.println();</span></span><br><span class="line">		<span class="comment">//tree.inThread();</span></span><br><span class="line">		<span class="comment">//tree.inTraversal();</span></span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//需要如何创建二叉树</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> CREATE_PRE=<span class="number">1</span>;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> CREATE_IN=<span class="number">2</span>;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> CREATE_POST=<span class="number">3</span>;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> CREATE_FOOR=<span class="number">4</span>;</span><br><span class="line">    <span class="comment">//是否为线索</span></span><br><span class="line">	<span class="keyword">private</span> Boolean THREAD=<span class="keyword">true</span>;</span><br><span class="line">	<span class="keyword">private</span> Boolean LINK=<span class="keyword">false</span>;</span><br><span class="line">	<span class="comment">//保存前驱结点</span></span><br><span class="line">	<span class="keyword">private</span> BTThreadNode&lt;T&gt; preNode;</span><br><span class="line">	<span class="keyword">private</span> BTThreadNode&lt;T&gt; root;</span><br><span class="line">    <span class="comment">/** setter ** getter **/</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> BTThreadNode&lt;T&gt; <span class="title">getRoot</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> root;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRoot</span><span class="params">(BTThreadNode&lt;T&gt; root)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.root = root;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//树的结点</span></span><br><span class="line">	<span class="meta">@SuppressWarnings</span>(<span class="string">"hiding"</span>)</span><br><span class="line">	<span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">BTThreadNode</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">		T data;</span><br><span class="line">		BTThreadNode&lt;T&gt; lchild, rchild;</span><br><span class="line">		Boolean lchTag, rchTag;</span><br><span class="line">		BTThreadNode(T a) &#123;</span><br><span class="line">			data=a;</span><br><span class="line">			lchild=<span class="keyword">null</span>;</span><br><span class="line">			rchild=<span class="keyword">null</span>;</span><br><span class="line">			lchTag=LINK;</span><br><span class="line">			rchTag=LINK;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//创建二叉树</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">createtBinaryTree</span><span class="params">(T []a, <span class="keyword">int</span> methed)</span></span>&#123;</span><br><span class="line">		<span class="keyword">switch</span>(methed)&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">1</span>:root=createPreBinaryTree(a, root, a.length, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">2</span>:root=createInBinaryTree(a, root, a.length, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">3</span>:root=createPostBinaryTree(a, root, a.length, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">4</span>:root=createFoorBinaryTree(a, root, a.length, <span class="number">0</span>); <span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 层次构造二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> BTThreadNode&lt;T&gt; <span class="title">createFoorBinaryTree</span><span class="params">(T []a, BTThreadNode&lt;T&gt; node, <span class="keyword">int</span> len, <span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">		LinkedTransferQueue&lt;BTThreadNode&lt;T&gt;&gt; queue=<span class="keyword">new</span> LinkedTransferQueue&lt;BTThreadNode&lt;T&gt;&gt;();</span><br><span class="line">		BTThreadNode&lt;T&gt; e=<span class="keyword">null</span>, lc=<span class="keyword">null</span>, rc=<span class="keyword">null</span>;</span><br><span class="line">		e=<span class="keyword">new</span> BTThreadNode&lt;T&gt;(a[index++]);</span><br><span class="line">		node=e;</span><br><span class="line">		<span class="comment">//将根入队列</span></span><br><span class="line">		queue.add(e);</span><br><span class="line">		<span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">			<span class="comment">//引用队头元素</span></span><br><span class="line">			e=queue.peek();</span><br><span class="line">			<span class="comment">//数组全部构造完</span></span><br><span class="line">			<span class="keyword">if</span>(index&lt;len)&#123;</span><br><span class="line">				<span class="comment">//构造左孩子，入队列</span></span><br><span class="line">				lc=<span class="keyword">new</span> BTThreadNode&lt;T&gt;(a[index++]);</span><br><span class="line">				queue.add(lc);</span><br><span class="line">				e.lchild=lc;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(index&lt;len)&#123;</span><br><span class="line">				<span class="comment">//构造右孩子，入队列</span></span><br><span class="line">				rc=<span class="keyword">new</span> BTThreadNode&lt;T&gt;(a[index++]);</span><br><span class="line">				queue.add(rc);</span><br><span class="line">				e.rchild=rc;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//出队</span></span><br><span class="line">			queue.poll();</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> node;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 前构造二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> BTThreadNode&lt;T&gt; <span class="title">createPostBinaryTree</span><span class="params">(T []a, BTThreadNode&lt;T&gt; node, <span class="keyword">int</span> len, <span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(index&gt;=len)<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		node.lchild=createPostBinaryTree(a, node.lchild, len, index); <span class="comment">//递归构建左子树</span></span><br><span class="line">		node.rchild=createPostBinaryTree(a, node.rchild, len, index); <span class="comment">//递归构建右子树</span></span><br><span class="line">		T e = a[index++];</span><br><span class="line">		<span class="keyword">if</span>(e.toString()==<span class="string">"#"</span>)&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		node = <span class="keyword">new</span> BTThreadNode&lt;T&gt;(e) ;</span><br><span class="line">		<span class="keyword">return</span> node;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 中序构造二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> BTThreadNode&lt;T&gt; <span class="title">createInBinaryTree</span><span class="params">(T []a, BTThreadNode&lt;T&gt; node, <span class="keyword">int</span> len, <span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(index&gt;=len)<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		node.lchild=createInBinaryTree(a, node.lchild, len, index); <span class="comment">//递归构建左子树</span></span><br><span class="line">		T e = a[index++];</span><br><span class="line">		<span class="keyword">if</span>(e.toString()==<span class="string">"#"</span>)&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		node = <span class="keyword">new</span> BTThreadNode&lt;T&gt;(e) ;</span><br><span class="line">		node.rchild=createInBinaryTree(a, node.rchild, len, index); <span class="comment">//递归构建右子树</span></span><br><span class="line">		<span class="keyword">return</span> node;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 后序构造二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> BTThreadNode&lt;T&gt; <span class="title">createPreBinaryTree</span><span class="params">(T []a, BTThreadNode&lt;T&gt; node, <span class="keyword">int</span> len, <span class="keyword">int</span> index)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(index&gt;=len)<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		T e = a[index++];</span><br><span class="line">		<span class="keyword">if</span>(e.toString()==<span class="string">"#"</span>)&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		node = <span class="keyword">new</span> BTThreadNode&lt;T&gt;(e) ;</span><br><span class="line">		node.lchild=createPreBinaryTree(a, node.lchild, len, index); <span class="comment">//递归构建左子树</span></span><br><span class="line">		node.rchild=createPreBinaryTree(a, node.rchild, len, index); <span class="comment">//递归构建右子树</span></span><br><span class="line">		<span class="keyword">return</span> node;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 构建前序索引二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">preThread</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		preNode=<span class="keyword">null</span>;</span><br><span class="line">		preThread(root);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">preThread</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(node==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		<span class="comment">//左结点为空则将左指针指向前驱结点</span></span><br><span class="line">		<span class="keyword">if</span>(node.lchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			node.lchTag=THREAD;</span><br><span class="line">			node.lchild=preNode;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//右结点为空则将前驱结点指向当前结点</span></span><br><span class="line">		<span class="keyword">if</span>(preNode!=<span class="keyword">null</span>&amp;&amp;preNode.rchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			preNode.rchTag=THREAD;</span><br><span class="line">			preNode.rchild=node;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//更新前驱结点</span></span><br><span class="line">		preNode=node;</span><br><span class="line">		<span class="comment">//防止重复遍历构建前序线索二叉树导致堆栈溢出</span></span><br><span class="line">		<span class="keyword">if</span>(node.lchTag==LINK)preThread(node.lchild);</span><br><span class="line">		<span class="keyword">if</span>(node.rchTag==LINK)preThread(node.rchild);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 *  构建中序遍历二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inThread</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		preNode=<span class="keyword">null</span>;</span><br><span class="line">		inThread(root);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">inThread</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(node==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		inThread(node.lchild);</span><br><span class="line">		<span class="comment">//左结点为空则将左指针指向前驱结点</span></span><br><span class="line">		<span class="keyword">if</span>(node.lchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			node.lchTag=THREAD;</span><br><span class="line">			node.lchild=preNode;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//右结点为空则将前驱结点指向当前结点</span></span><br><span class="line">		<span class="keyword">if</span>(preNode!=<span class="keyword">null</span>&amp;&amp;preNode.rchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			preNode.rchTag=THREAD;</span><br><span class="line">			preNode.rchild=node;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//更新前驱结点</span></span><br><span class="line">		preNode=node;</span><br><span class="line">		<span class="comment">//防止重复遍历构建前序线索二叉树导致堆栈溢出</span></span><br><span class="line">		inThread(node.rchild);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 构建后续遍历二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postThread</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		postThread(root);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">postThread</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(node==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		<span class="keyword">if</span>(node.lchTag==LINK)postThread(node.lchild);</span><br><span class="line">		<span class="keyword">if</span>(node.rchTag==LINK)postThread(node.rchild);</span><br><span class="line">		<span class="comment">//左结点为空则将左指针指向前驱结点</span></span><br><span class="line">		<span class="keyword">if</span>(node.lchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			node.lchTag=THREAD;</span><br><span class="line">			node.lchild=preNode;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//右结点为空则将前驱结点指向当前结点</span></span><br><span class="line">		<span class="keyword">if</span>(preNode!=<span class="keyword">null</span>&amp;&amp;preNode.rchild==<span class="keyword">null</span>)&#123;</span><br><span class="line">			preNode.rchTag=THREAD;</span><br><span class="line">			preNode.rchild=node;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//更新前驱结点</span></span><br><span class="line">		preNode=node;</span><br><span class="line">		<span class="comment">//防止重复遍历构建前序线索二叉树导致堆栈溢出</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		System.out.print(node.data+<span class="string">""</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 前序遍历索引二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">preTraversal</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		preTraversal(root);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">preTraversal</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		BTThreadNode&lt;T&gt; cur = node;</span><br><span class="line">		<span class="keyword">while</span>(cur!=<span class="keyword">null</span>)&#123;</span><br><span class="line">			<span class="comment">//只要左不为空，不是前驱结点就一直向左访问</span></span><br><span class="line">			<span class="keyword">if</span>(cur.lchTag==LINK&amp;&amp;cur.lchild!=<span class="keyword">null</span>)&#123;</span><br><span class="line">				show(cur);</span><br><span class="line">				cur=cur.lchild;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="comment">//左为空就将右结点值为当前访问结点</span></span><br><span class="line">				show(cur);</span><br><span class="line">				cur=cur.rchild;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 中序遍历索引二叉树</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inTraversal</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		inTraversal(root);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">inTraversal</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		BTThreadNode&lt;T&gt; cur = node;</span><br><span class="line">		<span class="keyword">while</span>(cur!=<span class="keyword">null</span>)&#123;</span><br><span class="line">			<span class="comment">//一路向左找到最左的结点。</span></span><br><span class="line">			<span class="keyword">while</span> (cur.lchTag==LINK&amp;&amp;cur.lchild!=<span class="keyword">null</span>)cur = cur.lchild;</span><br><span class="line">			show(cur);</span><br><span class="line">			<span class="comment">//右孩子不为null就访问</span></span><br><span class="line">			<span class="keyword">while</span>(cur.rchild!=<span class="keyword">null</span>)&#123;</span><br><span class="line">				cur=cur.rchild;</span><br><span class="line">				show(cur);</span><br><span class="line">				<span class="comment">//如果右孩子不为后驱结点就将将右孩子置为当前结点</span></span><br><span class="line">				<span class="keyword">if</span>(cur.rchTag==LINK)&#123;</span><br><span class="line">					cur=cur.rchild;</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 后续遍历二叉树的反向</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postTraversal</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="keyword">return</span>;</span><br><span class="line">		postTraversal(root);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">postTraversal</span><span class="params">(BTThreadNode&lt;T&gt; node)</span></span>&#123;</span><br><span class="line">		BTThreadNode&lt;T&gt; cur = node;</span><br><span class="line">		<span class="keyword">while</span>(cur!=<span class="keyword">null</span>)&#123;</span><br><span class="line">			<span class="keyword">if</span>(cur.rchTag==LINK&amp;&amp;cur.rchild!=<span class="keyword">null</span>)&#123;</span><br><span class="line">				show(cur);</span><br><span class="line">				cur=cur.rchild;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				show(cur);</span><br><span class="line">				cur=cur.lchild;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
      





    


    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>如果这篇文章对您很有帮助，不妨</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/blog/images/wechatpay.png" alt="付祥/lfagon 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/blog/images/alipay.jpg" alt="付祥/lfagon 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
    
    	<div>
	   
             <div>
    
        <div style="margin-top: 30px;text-align:center;color: #ccc;font-size:14px;">-------------　　　　本文结束　<i class="fa fa-heart"></i>　感谢您的阅读　　　　-------------</div>
    
</div>
           
        </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/java/" rel="tag"><i class="fa fa-tag"></i> java</a>
          
            <a href="/blog/tags/algorithm/" rel="tag"><i class="fa fa-tag"></i> algorithm</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        
          <div class="wp_rating">
            <div style="color: rgba(0, 0, 0, 0.75); font-size:13px; letter-spacing:3px">(&gt;看完记得五星好评哦亲&lt;)</div>
            <div id="wpac-rating"></div>
          </div>
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/05/16/vue工程搭建/" rel="next" title="vue 工程搭建">
                <i class="fa fa-chevron-left"></i> vue 工程搭建
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/05/18/查找通过yum安装的程序安装目录/" rel="prev" title="查找通过 yum 安装的程序安装目录">
                查找通过 yum 安装的程序安装目录 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "24",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "7"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
  </div>


          </div>
          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/images/avatar.jpg" alt="付祥/lfagon">
            
              <p class="site-author-name" itemprop="name">付祥/lfagon</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">77</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/lfagon" target="_blank" title="Github">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/lfagon" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-git"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:slash7675@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i></a>
                  </span>
                
            </div>
          

          
          
            <div class="cc-license motion-element" itemprop="license">
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
                <img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons">
              </a>
            </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-globe"></i>
                圈子里住着一群可爱的程序猿&nbsp;<i class="fa fa-fw fa-globe fa-flip-horizontal"></i>
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://reuixiy.github.io/" title="一个Hexo博客玩到极致的大佬" target="_blank">一个Hexo博客玩到极致的大佬</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://mmmmasterc.github.io/" title="又菜又爱秀的哥们" target="_blank">又菜又爱秀的哥们</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.codesheep.cn/" title="乐于分享的华科Java程序羊(B站up主)" target="_blank">乐于分享的华科Java程序羊(B站up主)</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.idealli.com/" title="前端大神自称兰州小红鸡" target="_blank">前端大神自称兰州小红鸡</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.</span> <span class="nav-text">代码</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
    </div>
  </aside>


        
      </div>
    </div></main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <font color="red"><i class="fa fa-heartbeat" aria-hidden="true"></i></font>
  </span>
  <span class="author" itemprop="copyrightHolder">copyright</span>
  &nbsp;<a href="https://www.oschina.net/"><font size="2px">中国开源社区</font></a>

  
</div>




<div style="width:300px;margin:0 auto; padding:0 0;">
   <a href style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="/blog/images/icons/record.png" style="float:left;height:90%;width:auto;"><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;letter-spacing:0px"><font size="2px">公网安备 xxxxxxxxxxx号</font></p></a>
</div>





        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">&nbsp;访客量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">&nbsp;访问量：</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>




















  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  

  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/image.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/linkcard.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicksocialvalue.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/clicklove.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  



	





  




  





<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: window.location.pathname, 
            owner: 'lfagon',
            repo: 'BlogComment',
            
            lang: "" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: 'de7d4aee11c1d7e4fd0ab75222817a4875fb378c',
            
                client_id: '969cf8ab6443ea4bd042'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    





  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/blog/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/blog/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  
  <script type="text/javascript">
  wpac_init = window.wpac_init || [];
  wpac_init.push({widget: 'Rating', id: 18607,
    el: 'wpac-rating',
    color: 'f79533'
  });
  (function() {
    if ('WIDGETPACK_LOADED' in window) return;
    WIDGETPACK_LOADED = true;
    var mc = document.createElement('script');
    mc.type = 'text/javascript';
    mc.async = true;
    mc.src = '//embed.widgetpack.com/widget.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(mc, s.nextSibling);
  })();
  </script>


  

  
<script>
  $('.highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
